package com.jsyl.lwbk.web.controller.business;

import com.jsyl.lwbk.business.domain.TrainPackage;
import com.jsyl.lwbk.business.dto.TrainPackageDTO;
import com.jsyl.lwbk.business.service.TrainPackageService;
import com.jsyl.lwbk.common.core.controller.BaseController;
import com.jsyl.lwbk.common.core.domain.AjaxResult;
import com.jsyl.lwbk.common.core.page.TableDataInfo;
import com.jsyl.lwbk.common.utils.DateUtils;
import com.jsyl.lwbk.framework.util.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 训练包
 */
@Controller
@RequestMapping("/business/trainPackage")
public class TrainPackageController extends BaseController {

    private String prefix = "business/trainPackage";

    @Autowired
    TrainPackageService trainPackageService;


    @GetMapping()
    public String child(ModelMap mmap) {
        return prefix + "/trainPackageList";
    }


    @GetMapping("/detail/{trainPackageId}")
    public String child(@PathVariable Long trainPackageId,ModelMap mmap) {
        mmap.put("parentTrainId",trainPackageId);
        return prefix + "/trainPackageDetail";
    }

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo findList(TrainPackageDTO dto) {
        startPage();
        List<TrainPackageDTO> list = trainPackageService.findList(dto);
        return getDataTable(list);
    }

    @PostMapping("/detailList")
    @ResponseBody
    public TableDataInfo detailList(TrainPackage trainPackage) {
        startPage();
        List<TrainPackageDTO> list = trainPackageService.detailList(Long.valueOf(trainPackage.getParentTrainId()));
        return getDataTable(list);
    }


    /**
     * 页面跳转-新增训练包
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/addTrainPackage";
    }
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(trainPackageService.deleteById(ids));
    }


    /**
     * 获取父级训练包
     *
     * @return
     */
    @GetMapping("/getParentTrainPackage")
    @ResponseBody
    public List<TrainPackageDTO> getParentTrainPackage() {
        List<TrainPackageDTO> list = new ArrayList<>();
        list = trainPackageService.getParentTrainPackage();
        return list;
    }

    /**
     *
     *
     * @param trainPackage
     * @return
     */
    @PostMapping("/checkTrainNameUnique")
    @ResponseBody
    public String checkPlanNameUnique(TrainPackage trainPackage) {
        Integer temp =trainPackageService.checkTrainNameUnique(trainPackage);
        return temp.toString() ;
    }

    /**
     * 新增
     *
     * @param
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addClassPlan(TrainPackage trainPackage) {
        trainPackage.setCreateUser(ShiroUtils.getLoginName());
        trainPackage.setUpdateUser(ShiroUtils.getLoginName());
        trainPackage.setCreateTime(DateUtils.getNowDate());
        trainPackage.setUpdateTime(DateUtils.getNowDate());
        trainPackageService.addTrainPackage(trainPackage);
        return success();
    }

}
